import 'dart:convert';

import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:top_foundation/component/dialog/index.dart';

import '../../../../../../../helper/utils/util.dart';
import '../utils/ifly/ifly_util.dart';
import 'record_add_item.dart';

class AudioView extends StatelessWidget {
  const AudioView({
    Key? key,
    this.height,
    this.result,
  }) : super(key: key);
  final Function(AudioListModel)? result;
  final double? height;

  Future<bool> getPermisson() async {
    var isGranted = await Permission.microphone.isGranted;
    if (!isGranted) {
      var res = await [
        Permission.microphone,
      ].request();
      if (res[Permission.microphone] == PermissionStatus.granted) {
        return true;
      }
      return false;
    }

    return true;
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () async {
        if (!await getPermisson()) {
          // Hud.toast('请前往设置打开麦克风权限');
          showAlert(
            msg: Ls.openmic,
          ).then((value) {
            if (value == 1) {
              AppSettings.openAppSettings(type: AppSettingsType.settings);
            }
          });
          return;
        }
        IFlyUtil().ifly_audio().then((value) {
          if (result != null && value.dataJson.isNotEmpty) {
            result!(AudioListModel.fromJson(value.dataJson));
          }
        });
      },
      child: Container(
        height: height,
        color: const Color(0x55efefef),
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 8.r),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 80.r,
              height: 80.r,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(40.r),
              ),
              child: Image.memory(
                base64Decode(
                    "iVBORw0KGgoAAAANSUhEUgAAADoAAABGCAMAAACqhXegAAAAwFBMVEVHcEzh4eZYWGe4ucKurrfT09rDxMzJytHExc3Fxs65u8O5u8Pa2uDc2+Hc3OHFxs7DxMvOztXi4eZtbXzc3OLJytC3uMGpqrSXmaSFhZN2d4VfYG9VVWWHiJWAgY9XV2dyc4GHiJVtb31vcH7Gx8/Cw8uEhZLKy9J7fIrOztXk4+jR0dhoaXe0tr/X191/gI6+w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W9Y1AshAtX3bRbMRVuGx0ZF9KhnSlw1mJXspU0AF6QxzP0VJcxBNZjn/7IHX5+N+L9OM1J5KE3KZYP3Rf6npuboIUNzhxRleayKKiKMQRAMio04U2u9mrmkog/zEBY6vOKgaJffWakS3a2W5eHDGIbH8HicVDf8oMGCtg6rdmYBMqyXK2IU8OUPus5jOifD49C4OPesORkEC6OVnPAJA6R++YGxFrL+fNYiOT1JUmt+ZmzFQPR9dXkFXGVzrakpal7kw1Q1aw8Yk2m21cOx3tLNwC/GxHxhzjRN15aT1bjc6g/th0R9W2z1w0djvw6XnQfimdo2h0Pz1gl+xOHmHB6bRp9fK/pKre+c3Urr//PKWJqLrbrb7cZTc6kr/+oytKurD6peG6an06QHOq9N/2IimnegF3PYvE/1UMlhH3Sl5munjnfjfDV9+h+FlVnDmaS7xwAAAABJRU5ErkJggg=="),
                width: 50.r,
                height: 50.r,
              ),
            ),
            SizedBox(
              height: 8.r,
            ),
            Text(
              Ls.clickRecord,
              style: TextStyle(
                color: ColorsUtil.title,
                fontSize: 16.sp,
              ),
            )
          ],
        ),
      ),
    );
  }
}
